package com.hkts.management.unattended.poundroom.service.serviceimpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hkts.management.unattended.order.entity.HktsOrderInfo;
import com.hkts.management.unattended.poundroom.entity.YbMutualCompany;
import com.hkts.management.unattended.poundroom.mapper.YbMutualCompanyMapper;
import com.hkts.management.unattended.poundroom.service.IYbMutualCompanyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zy
 * @since 2021-10-13
 */
@Service
@Transactional
public class YbMutualCompanyServiceImpl extends ServiceImpl<YbMutualCompanyMapper, YbMutualCompany> implements IYbMutualCompanyService {


    @Autowired
    private YbMutualCompanyMapper ybMutualCompanyMapper;

    @Override
    public IPage<YbMutualCompany> selectPage(Page<YbMutualCompany> page, YbMutualCompany ybMutualCompany) {
        QueryWrapper<YbMutualCompany> query = Wrappers.query();
        QueryWrapper<YbMutualCompany> entityWrapper = getEntityWrapper(query, ybMutualCompany);
        return ybMutualCompanyMapper.selectPage(page, entityWrapper);
    }


    @Override
    public List<YbMutualCompany> selectList(YbMutualCompany ybMutualCompany) {
        QueryWrapper<YbMutualCompany> query = Wrappers.query();
        QueryWrapper<YbMutualCompany> entityWrapper = getEntityWrapper(query, ybMutualCompany);
        return ybMutualCompanyMapper.selectList(entityWrapper);
    }


    /**
     * 公共查询条件
     *
     * @param entityWrapper
     * @return
     */
    public QueryWrapper<YbMutualCompany> getEntityWrapper(QueryWrapper<YbMutualCompany> entityWrapper, YbMutualCompany ybMutualCompany) {
        //条件拼接
        if (StringUtils.isNotBlank(ybMutualCompany.getcComName())) {
            entityWrapper.like(YbMutualCompany.CCOMNAME, ybMutualCompany.getcComName());
        }
        if (StringUtils.isNotBlank(ybMutualCompany.getcComType())) {
            entityWrapper.like(YbMutualCompany.CCOMTYPE, ybMutualCompany.getcComType());
        }
        if (StringUtils.isNotBlank(ybMutualCompany.getbDel())) {
            entityWrapper.like(YbMutualCompany.BDEL, ybMutualCompany.getbDel());
        }

        return entityWrapper;
    }

    public LocalDateTime getLastMutualCompany(){
        QueryWrapper<YbMutualCompany> query = Wrappers.query();
        query.lambda().orderByDesc(YbMutualCompany::getDoptTime);
        query.last("limit 1");
        List<YbMutualCompany> ybMutualCompanies=ybMutualCompanyMapper.selectList(query);
        return ybMutualCompanies.size()>0?ybMutualCompanies.get(0).getDoptTime():null;
    }
    public void mysqlInsertOrUpdateBath(List<YbMutualCompany> ybMutualCompanies){
        ybMutualCompanyMapper.mysqlInsertOrUpdateBath(ybMutualCompanies);
    }
}
